package com.zxx.otherchaos.widgets.menuItem;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zxx.otherchaos.R;

import java.util.List;
import java.util.Random;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author zxx on 2021/7/12
 */
public class ArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int HEADER_TYPE = 0;
    List<Article> mArticles;
    OnItemClickListener<Article> mClickListener;
    private Context mContext;

    public ArticleAdapter(Context context, List<Article> mArticles) {
        this.mContext = context;
        this.mArticles = mArticles;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HEADER_TYPE) {
            return creteHeaderViewHolder(parent);
        }
        return createArticleViewHolder(parent);
    }

    //头部
    private RecyclerView.ViewHolder creteHeaderViewHolder(ViewGroup parent) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_adapter_header, parent, false);
        return new ArticleHeaderHolder(itemView);
    }

    //内容
    private RecyclerView.ViewHolder createArticleViewHolder(ViewGroup parent) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_adapter_activity, parent, false);
        return new ArticleViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ArticleViewHolder) {
            bindViewForArticle(holder, position);
        } else if (holder instanceof ArticleHeaderHolder) {
            bindHeaderView(holder);
        }
    }

    //头部
    private void bindHeaderView(RecyclerView.ViewHolder holder) {
        ArticleHeaderHolder headerHolder = (ArticleHeaderHolder) holder;

        Log.e("Tag-->", "店家实际1111");
        headerHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Tag-->", "店家实际");
                Toast.makeText(mContext, "头部", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //内容
    private void bindViewForArticle(RecyclerView.ViewHolder holder, int position) {
        ArticleViewHolder articleViewHolder = (ArticleViewHolder) holder;

        //瀑布流布局设置不同宽高
        ViewGroup.LayoutParams params = articleViewHolder.itemView.getLayoutParams();
        params.height = new Random().nextInt(10) * 50 + 100;
        articleViewHolder.itemView.setLayoutParams(params);

        final Article article = getItem(position);
        articleViewHolder.tvTitle.setText(article.title);
        articleViewHolder.publishTime.setText(article.time);
        articleViewHolder.authorTv.setText(article.author);

        articleViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null) {
                    mClickListener.onClick(article);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (HEADER_TYPE == position) {
            return HEADER_TYPE;
        }
        return 1;
    }

    private Article getItem(int position) {
        //因为第一项试图是header，添加了一个Header，因此索引减去1
        return mArticles.get(position - 1);
    }


    //多了一个header，数量要加1
    @Override
    public int getItemCount() {
        return mArticles == null ? 1 : mArticles.size() + 1;
    }

    public class ArticleViewHolder extends RecyclerView.ViewHolder {

        public TextView tvTitle;
        public TextView publishTime;
        public TextView authorTv;

        public ArticleViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            publishTime = itemView.findViewById(R.id.tv_time);
            authorTv = itemView.findViewById(R.id.tv_author);
        }
    }

    public interface OnItemClickListener<T> {
        void onClick(T t);
    }

    public void setmClickListener(OnItemClickListener<Article> mClickListener) {
        this.mClickListener = mClickListener;
    }

    //-------------------------
    public class ArticleHeaderHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;

        public ArticleHeaderHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_header);
        }
    }

}
